import sun.applet.Main;

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-03-31
 * Time: 21:23
 */
public class Demo1 {

    static int count = 0;
    static Object object = new Object();

    public static void main1(String[] args) throws InterruptedException {

        Vector<Integer> vector = new Vector<>();
        List<Integer> list = Collections.synchronizedList(new ArrayList<>());
        CopyOnWriteArrayList<Integer> onWriteArrayList = new CopyOnWriteArrayList<>();


        vector.add(0);
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                synchronized (vector) {
                    vector.set(0, vector.get(0) + 1);
                }
            }
        });
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                synchronized (vector) {
                    vector.set(0, vector.get(0) + 1);
                }
            }
        });
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println(vector);
    }



    public static void main2(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(10); // 代表有十个线程参加下载任务~
        for (int i = 0; i < 9; i++) {
            Thread thread = new Thread(() -> {
                for (int j = 0; j < 10_0000; j++) {
                    synchronized (object) {
                        count++;
                    }
                }
                countDownLatch.countDown();//线程安全的~
            });
            thread.start();
        }
        countDownLatch.await();//涉及阻塞都会抛出这个，被中断异常
        Thread thread = new Thread(() -> {
            for (int j = 0; j < 10_0000; j++) {
                synchronized (object) {
                    count++;
                }
            }
            countDownLatch.countDown();//线程安全的~
        });
        thread.start();
        System.out.println(count);
    }










    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 1; i <= 1000; i++) {
                    sum += i;
                }
                return sum;
            }
        };
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println(futureTask.get());
    }
}
